/** layuiAdmin.std-v1.2.1 LPPL License By http://www.layui.com/admin/ */
/**
 * 将日期格式化成指定格式的字符串
 * param date 要格式化的日期，不传时默认当前时间，也可以是一个时间戳
 * param fmt 目标字符串格式，支持的字符有：y,M,d,q,w,H,h,m,S，默认：yyyy-MM-dd HH:mm:ss
 * returns 返回格式化后的日期字符串
 */
function dateFormat(date, fmt)
{
    date = date == undefined ? new Date() : date;
    date = typeof date == 'number' ? new Date(date) : date;
    fmt = fmt || 'yyyy-MM-dd HH:mm:ss';
    var obj =
        {
            'y': date.getFullYear(), // 年份，注意必须用getFullYear
            'M': date.getMonth() + 1, // 月份，注意是从0-11
            'd': date.getDate(), // 日期
            'q': Math.floor((date.getMonth() + 3) / 3), // 季度
            'w': date.getDay(), // 星期，注意是0-6
            'H': date.getHours(), // 24小时制
            'h': date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, // 12小时制
            'm': date.getMinutes(), // 分钟
            's': date.getSeconds(), // 秒
            'S': date.getMilliseconds() // 毫秒
        };
    var week = ['天', '一', '二', '三', '四', '五', '六'];
    for(var i in obj)
    {
        fmt = fmt.replace(new RegExp(i+'+', 'g'), function(m)
        {
            var val = obj[i] + '';
            if(i == 'w') return (m.length > 2 ? '星期' : '周') + week[val];
            for(var j = 0, len = val.length; j < m.length - len; j++) val = '0' + val;
            return m.length == 1 ? val : val.substring(val.length - m.length);
        });
    }
    return fmt;
}

;
layui.define(["table", "form"],
    function(t) {
        function getQueryVariable(variable)
        {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++) {
                var pair = vars[i].split("=");
                if(pair[0] == variable){return pair[1];}
            }
            return(false);
        }
        var jobName = getQueryVariable("jobName");
        var jobType = getQueryVariable("jobType");
        console.log("------------------- "+jobName);
        var e = layui.$,
            $ = layui.$,
            i = layui.table,
            n = layui.form;
        i.render({
            elem: "#LAY-app-content-list",
            //url: layui.setter.base + "/api/jenkins/buildHistory?jobName=erp-stag.zfzj.api",
            url: "/api/jenkins/buildHistory?jobName="+jobName,
            //url: layui.setter.base + "json/content/list.js",
            cols: [[{
                type: "numbers",
                fixed: "left"
            },
                {
                    field: "tag",
                    title: "镜像 TAG",
                    width: 150,
                },
                {
                    field: "status",
                    title: "构建状态",
                    width: 110,
                    align: "center",
                    templet: "#statusTpl"
                },
                // {
                //     field: "progress",
                //     title: "进度",
                //     width: 200,
                //     align: "center",
                //     templet: "#progressTpl"
                // },
                {
                    field: "tag",
                    title: "代码 TAG",
                    width: 150,
                },
                {
                    field: "commit",
                    minWidth: 50,
                    title: "Commit(Git最新值)"
                },
                {
                    field: "builder",
                    title: "构建人",
                    minWidth: 100
                },
                {
                    field: "publishType",
                    title: "类型",
                    minWidth: 100
                },
                {
                    field: "start",
                    title: "构建时间",
                    width: 165,
                    templet: '<div>{{dateFormat(parseInt(d.start),"yyyy-MM-dd HH:mm:ss")}}</div>'
                },
                {
                    title: "操作",
                    width: 125,
                    align: "center",
                    fixed: "right",
                    toolbar: "#table-content-list"
                }]],
            page: !0,
            limit: 10,
            limits: [10, 15, 20, 25, 30],
            text: "对不起，加载出现异常！"
        }),
            i.on("tool(LAY-app-content-list)",
                function(t) {
                    var e = t.data;
                    //console.log("--------- "+JSON.stringify(t));
                    //console.log("1--------- "+JSON.stringify(e));
                    "rollback" === t.event ? layer.confirm("确定发布"+e.tag+"？",
                        function(e) {
                            // if(jobType === "beta"){
                            //     layer.msg("beta环境不支持回滚",{ icon: 0 ,time: 2000 });
                            //     return;
                            // }
                            var postData = {"jobName":jobName,
                                "tagName":t.data.tag,
                                "publishType":"publish",
                                "number":t.data.buildNumber
                            };
                            //提交 Ajax 成功后，关闭当前弹层并重载表格
                            $.ajax({
                                url: "/api/jenkins/buildJob",
                                type: 'post',
                                contentType:"application/json",
                                dataType:'json',
                                data: JSON.stringify(postData),
                                success: function (data, textStatus) {
                                    //console.log(data);
                                    if (textStatus == 'success') {
                                        if (typeof(data.code) == "undefined") {
                                            console.log("----code = undefined----"+data.code);
                                            //登入成功的提示与跳转
                                            layer.msg('登录状态已过期', {offset: '15px', icon: 2, time: 1000},
                                                function () {
                                                    location.href = '/';
                                                });
                                        }
                                        if(data.code != 0) {
                                            layer.msg(data.msg,{ icon: 0 ,time: 2000 });
                                            console.log(data.msg);
                                        }
                                    }
                                }
                            })
                            i.reload('LAY-app-content-list');
                            n.render(),layer.close(e)
                        }) : "selectLog" === t.event &&
                        layer.open({
                            type: 2,
                            title: "日志信息",
                            content: "/app/content/log.html?jobName=" + jobName + "&buildNumber=" + e.buildNumber,
                            maxmin: false,
                            area: ["800px", "450px"],
                            //btn: ["确定", "取消"],
                            yes: function(e, i) {
                                // var l = window["layui-layer-iframe" + e],
                                //     a = i.find("iframe").contents().find("#layuiadmin-app-form-edit");
                                // l.layui.form.on("submit(layuiadmin-app-form-edit)",
                                //     function(i) {
                                //         var l = i.field;
                                //         t.update({
                                //             label: l.label,
                                //             title: l.title,
                                //             author: l.author,
                                //             status: l.status
                                //         }),
                                //             n.render(),
                                //             layer.close(e)
                                //     }),
                                //     a.trigger("click")
                            }
                        })
                }),
            i.render({
                elem: "#LAY-app-content-tags",
                url: layui.setter.base + "json/content/tags.js",
                cols: [[{
                    type: "numbers",
                    fixed: "left"
                },
                    {
                        field: "id",
                        width: 100,
                        title: "ID",
                        sort: !0
                    },
                    {
                        field: "tags",
                        title: "分类名",
                        minWidth: 100
                    },
                    {
                        title: "操作",
                        width: 150,
                        align: "center",
                        fixed: "right",
                        toolbar: "#layuiadmin-app-cont-tagsbar"
                    }]],
                text: "对不起，加载出现异常！"
            }),
            i.on("tool(LAY-app-content-tags)",
                function(t) {
                    var i = t.data;
                    if ("del" === t.event) layer.confirm("确定删除此分类？",
                        function(e) {
                            t.del(),
                                layer.close(e)
                        });
                    else if ("edit" === t.event) {
                        e(t.tr);
                        layer.open({
                            type: 2,
                            title: "编辑分类",
                            content: "/app/content/tagsform.html?id=" + i.id,
                            area: ["450px", "200px"],
                            btn: ["确定", "取消"],
                            yes: function(e, i) {
                                var n = i.find("iframe").contents().find("#layuiadmin-app-form-tags"),
                                    l = n.find('input[name="tags"]').val();
                                l.replace(/\s/g, "") && (t.update({
                                    tags: l
                                }), layer.close(e))
                            },
                            success: function(t, e) {
                                var n = t.find("iframe").contents().find("#layuiadmin-app-form-tags").click();
                                n.find('input[name="tags"]').val(i.tags)
                            }
                        })
                    }
                }),
            i.render({
                elem: "#LAY-app-content-comm",
                url: layui.setter.base + "json/content/comment.js",
                cols: [[{
                    type: "checkbox",
                    fixed: "left"
                },
                    {
                        field: "id",
                        width: 100,
                        title: "ID",
                        sort: !0
                    },
                    {
                        field: "reviewers",
                        title: "评论者",
                        minWidth: 100
                    },
                    {
                        field: "content",
                        title: "评论内容",
                        minWidth: 100
                    },
                    {
                        field: "commtime",
                        title: "评论时间",
                        minWidth: 100,
                        sort: !0
                    },
                    {
                        title: "操作",
                        width: 150,
                        align: "center",
                        fixed: "right",
                        toolbar: "#table-content-com"
                    }]],
                page: !0,
                limit: 10,
                limits: [10, 15, 20, 25, 30],
                text: "对不起，加载出现异常！"
            }),
            i.on("tool(LAY-app-content-comm)",
                function(t) {
                    t.data;
                    "del" === t.event ? layer.confirm("确定删除此条评论？",
                        function(e) {
                            t.del(),
                                layer.close(e)
                        }) : "edit" === t.event && layer.open({
                        type: 2,
                        title: "编辑评论",
                        content: "../../../views/app/content/contform.html",
                        area: ["450px", "300px"],
                        btn: ["确定", "取消"],
                        yes: function(t, e) {
                            var n = window["layui-layer-iframe" + t],
                                l = "layuiadmin-app-comm-submit",
                                a = e.find("iframe").contents().find("#" + l);
                            n.layui.form.on("submit(" + l + ")",
                                function(e) {
                                    e.field;
                                    i.reload("LAY-app-content-comm"),
                                        layer.close(t)
                                }),
                                a.trigger("click")
                        },
                        success: function(t, e) {}
                    })
                }),
            t("contlist", {})
    });